package org.bonorsoft.modules.wechat.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.bonorsoft.common.api.vo.Result;
import org.bonorsoft.common.aspect.annotation.AutoLog;
import org.bonorsoft.common.system.base.controller.BonorController;
import org.bonorsoft.common.system.query.QueryGenerator;
import org.bonorsoft.common.system.vo.LoginUser;
import org.bonorsoft.common.util.oConvertUtils;
import org.bonorsoft.modules.wechat.entity.WxRequest;
import org.bonorsoft.modules.wechat.service.IEchoPhotoService;
import org.bonorsoft.modules.wechat.service.IWxRequestService;
import org.bonorsoft.modules.wechat.service.IWxResponseService;
import org.bonorsoft.modules.wechat.service.impl.EchoPhotoServiceImpl;
import org.bonorsoft.modules.wechat.vo.WxRequestVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.UUID;

/**
 * 呼应 请求信息
 */
@Slf4j
@Controller
@RequestMapping("/wechat/request")
public class WxRequestController extends BonorController<WxRequest, IWxRequestService> {

    @Autowired
    private IWxRequestService wxRequestService;

    @Autowired
    private IWxResponseService wxResponseService;

    @Autowired
    private IEchoPhotoService echoPhotoService;

    /**
     * 呼应分页列表查询
     * @param wxRequest
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "呼应-分页列表查询")
    @RequiresPermissions("wechat:request:list")
    @GetMapping(value = "/list")
    @ResponseBody
    public Result<?> queryPageList(WxRequest wxRequest,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req){
        QueryWrapper<WxRequest> queryWrapper = QueryGenerator.initQueryWrapper(wxRequest,req.getParameterMap());
        Page<WxRequest> page = new Page<>(pageNo, pageSize);
        IPage<WxRequest> pageList = wxRequestService.paging(page,queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 呼应 请求 添加
     * @param wxRequest
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Result<?> add(@RequestBody WxRequestVo wxRequest){
        String photo = wxRequest.getPhoto();
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();//取得当前用户
        String qqid = UUID.randomUUID().toString().replaceAll("-", "");
        wxRequest.setQqid(qqid).setTjsj(new Date()).setTjrid(user.getUsername()).setTjrxm(user.getRealname());
        wxRequestService.save(wxRequest);
        if(oConvertUtils.isNotEmpty(photo)){
            String[] split;
            if(photo.contains(",")){
                split = photo.split(",");
            }else {
                split = new String[]{photo};
            }
            for (String s : split) {
                echoPhotoService.addPhoto(qqid,s);
            }
        }
        return Result.ok("添加成功！");
    }

    /**
     * 呼应 详细
     * @param req
     * @return
     */
    @GetMapping("/detail")
    @ResponseBody
    public Result<?> detail(HttpServletRequest req){
        String id = req.getParameter("id");
        Result<JSONObject> result = new Result<>();
        JSONObject obj = new JSONObject();
        obj.put("request",wxRequestService.getById(id));
        obj.put("photo",echoPhotoService.getPhoto(id));
        obj.put("response",wxResponseService.getWxResponseByQqid(id));
        result.setResult(obj);
        result.success("查询成功！");
        return result;
    }
}
